unit check_5form;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, BCButton, BCLabel, Forms, Controls, Graphics,
  Dialogs, DateUtils, fpjson, jsonparser, ExtCtrls, StdCtrls, ComCtrls,
  nkTitleBar, Math;

type

  { TfrmCheck_5 }

  TfrmCheck_5 = class(TForm)
    BCButton1: TBCButton;
    BCLabel1: TBCLabel;
    Label1: TLabel;
    Label2: TLabel;
    Label7: TLabel;
    Label8: TLabel;
    ls: TListView;
    Memo1: TMemo;
    nkTitleBar1: TnkTitleBar;
    Panel5: TPanel;
    Panel6: TPanel;
    Panel7: TPanel;
    Panel8: TPanel;
    procedure BCButton1Click(Sender: TObject);
    procedure BCLabel1Click(Sender: TObject);
    procedure FormShow(Sender: TObject);
  private

  public
    procedure Log(AText:string);
  end;

var
  frmCheck_5: TfrmCheck_5;

implementation

uses
  datamodule;

{$R *.frm}

{ TfrmCheck_5 }

procedure TfrmCheck_5.FormShow(Sender: TObject);
var
  mReports:string;
begin
  //填充待分析月份列表
  //获取以分析月份列表，组合成字符串
  with dm.pubqryWork do
  begin
    {Sql.Clear;
    Sql.Add('select distinct cyear,cmonth from huabofx');
    Open;
    First;
    mReports:='';
    while not Eof do
    begin
      mReports:=mReports+inttostr(FieldByName('cyear').AsInteger)+inttostr(FieldByName('cmonth').AsInteger)+';';
      Next;
    end;}
    Sql.Clear;
    Sql.Add('select distinct cyear,cmonth from fxmaster where shortkind="职工医疗个人账户划拨风险"');
    Open;
    First;
    mReports:='';
    while not Eof do
    begin
      mReports:=mReports+inttostr(FieldByName('cyear').AsInteger)+inttostr(FieldByName('cmonth').AsInteger)+';';
      Next;
    end;
  end;
  //获取划拨数据的月份列表
  ls.Items.BeginUpdate;
  ls.Items.Clear;
  with dm.pubqryWork do
  begin
    Sql.Clear;
    Sql.Add('select distinct cyear, cmonth from huabo order by cyear desc, cmonth desc');
    Open;
    First;
    while not Eof do
    begin
      with ls.Items.Add do
      begin
        Caption:=inttostr(FieldByName('cyear').AsInteger)+'年'+inttostr(FieldByName('cmonth').AsInteger)+'月';
        SubItems.Add(inttostr(FieldByName('cyear').AsInteger));
        SubItems.Add(inttostr(FieldByName('cmonth').AsInteger));
        if Pos(SubItems.Strings[0]+SubItems.Strings[1], mReports) > 0 then
        begin
          SubItems.Add('1');
          ImageIndex:=1;
          Checked:=False;
        end
        else
        begin
          SubItems.Add('0');
          ImageIndex:=0;
          Checked:=True;
        end;
      end;
      Next;
    end;
  end;
  ls.Items.EndUpdate;

end;

procedure TfrmCheck_5.Log(AText: string);
begin
  Memo1.Lines.Add(AText);
  Memo1.Update;
  Application.ProcessMessages;
end;

procedure TfrmCheck_5.BCButton1Click(Sender: TObject);
var
  mChecked:boolean;
  i:integer;
  mYear,mMonth:integer;
  mR:TnkReport;
  mSiwangDate,mHuaboDate:TDate;
  mSiwangDate2:TDate;
  mDelete:boolean;
  mJson:TJsonObject;
begin
  //先检查有没有Checked项目
  mchecked:=False;
  for i:=0 to ls.Items.Count-1 do
  begin
    if ls.Items.Item[i].Checked then
    begin
      mchecked:=True;
      break;
    end;
  end;
  if mchecked=False then
  begin
    MessageDlg('请选择要分析的月份！',mtWarning,[mbOK],0);
    Exit;
  end;
  //开始分析个人账户划拨风险，第一步挑出同时出现在划拨名单和死亡名单里的人，第二步比对是否符合划拨条件（次月很当月的问题）
  ls.Enabled:=False;
  BCButton1.Enabled:=False;
  Memo1.Lines.Clear;
  Log('开始分析职工个人医疗账户划拨风险……');
  for i:=0 to ls.Items.Count-1 do
  begin
    if ls.Items.Item[i].Checked then
    begin
      Log('==========================');
      Log('分析'+ls.Items.Item[i].Caption+'划拨数据……');
      mYear:=strtoint(ls.Items.Item[i].SubItems.Strings[0]);
      mMonth:=strtoint(ls.Items.Item[i].SubItems.Strings[1]);
      mDelete:=Trim(ls.Items.Item[i].SubItems.Strings[2])='1';
      mR:=TnkReport.Create(Self);
      mR.CYear:=mYear;
      mR.CMonth:=mMonth;
      mR.TipsCount:=0;
      mR.WarningCount:=0;
      mR.Title:='职工个人医疗账户划拨风险分析';

      with dm.pubqryWork do
      begin
        Sql.Clear;
        Sql.Add('select * from huabo where cyear=:cyear and cmonth=:cmonth and c0 in (select c2 from siwang)');
        Params.ParamByName('cyear').AsInteger:=mYear;
        Params.ParamByName('cmonth').AsInteger:=mMonth;
        Open;
        if RecordCount = 0 then
        begin
          Log('没有发现对死亡人员划拨数据。');
        end
        else
        begin
          Log('发现'+inttostr(RecordCount)+'条对死亡人员划拨数据，正在继续分析……');
          First;
          while not Eof do
          begin
            //检查划拨日期与死亡日期应于同一月份，是则生成提示，否则判断为风险
            if Dm.GetSiwangDate(FieldByName('c0').AsString, mSiwangDate) then
            begin
              mHuaBoDate:=EncodeDatetime(FieldByName('cyear').AsInteger,FieldByName('cmonth').AsInteger,1,1,1,1,1);
              if mHuaboDate> mSiwangDate then
              begin
                mSiwangDate2:=EncodeDatetime(YearOf(mSiwangDate), MonthOf(mSiwangDate),1,1,1,1,1);
                if MonthSpan(mHuaboDate,mSiwangDate2)>=1 then
                //if MonthSpan(mHuaboDate,mSiwangDate)>1 then
                begin
                  //Log(floattostr(MonthSpan(mSiwangDate,mHuaboDate)));
                  // >=1个月，为风险
                  Log('划拨风险! '+FieldByName('c0').AsString+'划拨日期超过死亡日期一个月');
                  mR.WarningCount:=mR.WarningCount+1;
                  mR.AddWarning(FieldByName('c0').AsString,inttostr(ceil(MonthSpan(mHuaboDate,mSiwangDate2))-1));
                  //添加详细信息
                  mJson:=TJsonObject.Create;
                  mJson.Add('huabo_id', inttostr(FieldByName('id').AsInteger));
                  mJson.Add('a0', FormatDatetime('yyyy-mm-dd', mSiwangDate));
                  mJson.Add('a1', FormatDatetime('yyyy-mm', mhuaboDate));
                  mJson.Add('a2', inttostr(ceil(MonthSpan(mHuaboDate,mSiwangDate2))-1));
                  mJson.Add('c0', FieldByName('c0').AsString);
                  mJson.Add('c1', FieldByName('c1').AsString);
                  mJson.Add('c2', FieldByName('c2').AsString);
                  mJson.Add('c3', FieldByName('c3').AsString);
                  mJson.Add('c4', FieldByName('c4').AsString);
                  mJson.Add('c5', FieldByName('c5').AsString);
                  mJson.Add('c6', FieldByName('c6').AsString);
                  mJson.Add('c8', FieldByName('c8').AsString);
                  mJson.Add('c9', FieldByName('c9').AsString);
                  mJson.Add('c10', FieldByName('c10').AsString);
                  mJson.Add('c11', FieldByName('c11').AsString);
                  mJson.Add('c12', FieldByName('c12').AsString);
                  mJson.Add('c13', FieldByName('c13').AsString);
                  mJson.Add('c14', FieldByName('c14').AsString);
                  mR.AddDetail(FieldByName('c0').AsString, StringReplace(mJson.AsJSON,'\','',[rfReplaceAll]));
                  mJson.Free;
                end
                else
                begin
                  //未超过1个月，为提示
                  //Log('风险提示: '+FieldByName('c0').AsString+'划拨日期与死亡日期同月');
                  mR.TipsCount:=mR.TipsCount+1;
                  mR.AddTips(FieldByName('c0').AsString,'划拨日期与死亡日期同月');
                  //添加详细信息
                  mJson:=TJsonObject.Create;
                  mJson.Add('huabo_id', inttostr(FieldByName('id').AsInteger));
                  mJson.Add('a0', FormatDatetime('yyyy-mm-dd', mSiwangDate));
                  mJson.Add('a1', FormatDatetime('yyyy-mm', mhuaboDate));
                  mJson.Add('c0', FieldByName('c0').AsString);
                  mJson.Add('c1', FieldByName('c1').AsString);
                  mJson.Add('c2', FieldByName('c2').AsString);
                  mJson.Add('c3', FieldByName('c3').AsString);
                  mJson.Add('c4', FieldByName('c4').AsString);
                  mJson.Add('c5', FieldByName('c5').AsString);
                  mJson.Add('c6', FieldByName('c6').AsString);
                  mJson.Add('c8', FieldByName('c8').AsString);
                  mJson.Add('c9', FieldByName('c9').AsString);
                  mJson.Add('c10', FieldByName('c10').AsString);
                  mJson.Add('c11', FieldByName('c11').AsString);
                  mJson.Add('c12', FieldByName('c12').AsString);
                  mJson.Add('c13', FieldByName('c13').AsString);
                  mJson.Add('c14', FieldByName('c14').AsString);
                  mR.AddDetail(FieldByName('c0').AsString, StringReplace(mJson.AsJSON,'\','',[rfReplaceAll]));
                  mJson.Free;
                end;
              end;
            end;
            Next;
          end;
        end;
      end;
      Log('本月风险点数量：'+inttostr(mR.WarningCount));//+'  提示点数量：'+inttostr(mR.TipsCount));
      //保存分析结果
      dm.SaveFx(mR,mDelete,'职工医疗个人账户划拨风险');
//      dm.SaveHuaboFx(mR,mDelete,'职工医疗个人账户划拨风险');
      dm.SaveAnalLog(Label1.Caption, mYear, mMonth, mR.WarningCount, mR.TipsCount);
      dm.UpdateAnalCount;
      mR.Free;
    end;
  end;
  Log('----分析完毕----');
  BCButton1.Enabled:=True;
  ls.Enabled:=True;
end;

procedure TfrmCheck_5.BCLabel1Click(Sender: TObject);
begin
  ModalResult:=mrCancel;
end;

end.

